package com.xxgc.wujiaoservice.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxgc.wujiaoservice.entity.GiftEntity;
import com.xxgc.wujiaoservice.entity.GiftEntityRepository;
import com.xxgc.wujiaoservice.entity.UsersEntity;
import com.xxgc.wujiaoservice.entity.UsersEntityRepository;
import com.xxgc.wujiaoservice.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 直播业务
 */
@RestController
@RequestMapping("/live")
@Tag(name = "直播相关")
public class LiveController {

    @Autowired
    private GiftEntityRepository giftEntityRepository;
    @Autowired
    private UsersEntityRepository usersEntityRepository;

    /**
     * 查询礼物接口
     */
    @GetMapping("/selectGift")
    public Result selectGift(){
        List<GiftEntity> list = giftEntityRepository.list();
        return Result.success(list);
    }

    /**
     * 获取当前登录用户的钱包余额
     */
    @GetMapping("/getUserGold")
    public Result getUserGold(){
        //通过请求头的token获取用户id

        int userId = StpUtil.getLoginIdAsInt();
        UsersEntity usersEntity = usersEntityRepository.getById(userId);
        return Result.success(usersEntity.getGold());
    }


    //快捷键 ctrl + shift + u
    private static double SYSTEM_PROPORTION = 1.5;

    //赠送礼物
    @PostMapping("/giveGift/{giftId}")
    public Result giveGift(@PathVariable String giftId){

        //礼物的id
        int userId = StpUtil.getLoginIdAsInt();

        // 查询当前用户信息
        UsersEntity user = usersEntityRepository.getById(userId);

        // 查询礼物信息
        GiftEntity gift = giftEntityRepository.getById(giftId);

        // 检查用户金币是否足够
        if (user.getGold() < gift.getMoney()) {
            return Result.fail(400, "金币不足，无法赠送礼物");
        }

        // 扣除用户金币
        int mewMoney = user.getGold() - gift.getMoney();
        user.setGold(mewMoney);
        usersEntityRepository.updateById(user);
        //贡献值
        double contribution = gift.getMoney() * 0.2 * SYSTEM_PROPORTION;
        user.setContribution(Long.valueOf((long)contribution + user.getContribution()));

        //经验值
        /**
         * (当前等级 * 当前等级 *100) * (10/等级)
         */

        //修改值
        usersEntityRepository.updateById(user);

        return Result.success();

    }

    @GetMapping("/getUserByContribution")
    public Result getUserByContribution(){
        QueryWrapper<UsersEntity> qw = new QueryWrapper<>();
        //条件构造器 获取用户数据通过贡献值 只要前20个
        qw.orderByDesc("contribution");

        //分页查询
        Page<UsersEntity> page = new Page<>(1, 20);
        Page<UsersEntity> usersEntityPage = usersEntityRepository.page(page, qw);

        return Result.success(usersEntityPage);
    }

    /**
     * 通过礼物id查询礼物详细信息
     */
    @GetMapping("/getGiftInfoById/{giftId}")
    public Result getGiftInfoById(@PathVariable String giftId) {
        GiftEntity giftEntity = giftEntityRepository.getById(giftId);
        return Result.success(giftEntity);
    }

}
